﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace wsErrorsLog
{
    public static class Utilities
    {
        public static Object GetDBValue(this Object o )
        {
            if (o == null)
                return DBNull.Value;
            else
                return o;
        }

        public static DateTime? ConverMilisecondsToDateTime(long miliseconds)
        {
            if (miliseconds == 0)
                return null;
            var baseDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            DateTime GMTTime = baseDate.AddMilliseconds(miliseconds);
            DateTime localTime = TimeZoneInfo.ConvertTime(GMTTime, TimeZoneInfo.Local);
            return localTime;
        }

        public static long? ConvertDateTime(DateTime? datetime)
        {
            long? datetimeLong = null;
            if (datetime.HasValue)
            {
                DateTime utcDateTime = datetime.Value.ToUniversalTime();
                var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                datetimeLong = Convert.ToInt64((utcDateTime - epoch).TotalSeconds * 1000);
            }
            return datetimeLong;
        }
    }

 
}
